package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

//菜品模块
@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //分页+条件查询
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize,String name) {
        //构建菜品条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(name),Dish::getName,name);
        //开启分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //分页查询
        page = dishMapper.selectPage(page,qw);


        //遍历菜品列表
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            //根据菜品id查询口味列表
            LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
            dqw.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dqw);
            //封装口味列表
            dish.setFlavors(dishFlavorList);
            //根据分类信息id出啊寻分类信息
            Category category = categoryMapper.selectById(dish.getCategoryId());
            //封装分类名称
            dish.setCategoryName(category.getName());
        }
        //返回分页对象
        return page;
    }


    //新增菜品
    @Override
    public void save(Dish dish) {
        //1.新增菜品基本信息
        log.info("新增之前:"+dish.getId());
        dishMapper.insert(dish);
        log.info("新增之后:"+dish.getId());//主键返回

        //2.遍历口味
        for (DishFlavor flavor : dish.getFlavors()) {
            //关联菜品id
            flavor.setDishId(dish.getId());
            //新增口味
            dishFlavorMapper.insert(flavor);
        }
    }


    //回显菜品
    @Override
    public Dish findById(Long id) {
        //1.先查菜品
        Dish dish = dishMapper.selectById(id);
        //2.根据菜品id查询口味
        //2-1构建条件
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,id);
        //2-2调用mapper
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw);
        //3.将口味设置到菜品
        dish.setFlavors(dishFlavors);
        //4.返回菜品
        return dish;
    }


    //修改菜品
    @Override
    public void updateById(Dish dish) {
        //1.修改菜品基本信息
        dishMapper.updateById(dish);
        //2.根据菜品id删除原来口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(qw);
        //3.遍历新增口味
        for (DishFlavor flavor : dish.getFlavors()) {
            //关联菜品id
            flavor.setDishId(dish.getId());
            //口味新增
            dishFlavorMapper.insert(flavor);
        }
    }


    //起售/停售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);
        //修改实体类
        Dish dish = new Dish();
        dish.setStatus(status);
        //调用mapper
        dishMapper.update(dish,qw);
    }


    //删除
    @Override
    public void deleteBatchIds(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            Integer status = dish.getStatus();
            if (status!=1){
                //先删菜品
                dishMapper.deleteById(id);

                //再删口味
                LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
                qw.in(DishFlavor::getDishId,id);
                dishFlavorMapper.delete(qw);
            }
        }
    }

    //根据分类查询菜品列表
    @Override
    public List<Dish> findListByCategoryId(Long categoryId) {
        //1.构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);//根据分类查询
        qw.eq(Dish::getStatus,1);//起售状态

        //2.调用mapper查询并返回
        return dishMapper.selectList(qw);
    }

    //菜品展示
    @Override
    public List<Dish> findList(Long categoryId, Integer status) {
        //查询该分类下起售的菜品有哪些
        //1-1构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        qw.eq(Dish::getStatus,status);
        //1-2mapper查询
        List<Dish> dishList = dishMapper.selectList(qw);
        //遍历菜品列表
        for (Dish dish : dishList) {
            //根据菜品id查询口味
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList
                    (new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, dish.getId()));
            //添加到菜品中
            dish.setFlavors(dishFlavors);
        }
        //返回结果
        return dishList;
    }

    //套餐查询
    @Override
    public List<Dish> find(String name) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(Dish::getName,name);
        qw.eq(Dish::getStatus,1);

        List<Dish> dishList = dishMapper.selectList(qw);

        return dishList;
    }





}
